define(function(require) {

    'use strict';

    var Backbone = require('backbone'),
        LinkPopover = require('components/pop/link'),
        ConfirmPopover = require('components/pop/confirm'),
        DeletePopover = require('components/pop/delete'),
        DeleteMultiplePopover = require('components/pop/delete_multiple'),
        RulePopover = require('components/pop/rule'),
        KeywordPopover = require('components/pop/keyword'),
        ReplyPopover = require('components/pop/reply'),
        NormalReplyPopover = require('components/pop/normal_reply'),
        UrlPopover = require('components/pop/url'),
        GoodsUrlPopover = require('components/pop/goods_url'),
        TextPopover = require('components/pop/text'),
        RenamePopover = require('components/pop/rename'),
        ChangeCategoryPopover = require('components/pop/change_category'),
        ChangeCategory2Popover = require('components/pop/change_category2'),
        HelpNotesPopover = require('components/pop/help_notes'),
        MemoPopover = require('components/pop/memo'),
        ChosenPopover = require('components/pop/chosen'),
        CategoryPopover = require('components/pop/category'),
        MsgCatPopover = require('components/pop/msgcat'),
        TagPopover = require('components/pop/tag'),
        FormActionsPopover = require('components/pop/form_actions_confirm'),
        SendGoodsPopover = require('components/pop/send_goods'),
        NotePopover = require('components/pop/note'),
        CubePopover = require('components/pop/cube'),
        TimerPopover = require('components/pop/timer'),
        FansTagPopover = require('components/pop/fans_tag'),
        FansLevelPopover = require('components/pop/fans_level'),
        QrcodePopover = require('components/pop/qrcode'),
        RuleReplyPopover = require('components/pop/rule_reply'),
        RuleMethodPopover = require('components/pop/rule_method'),
        ChangePopover = require('components/pop/change'),
        AppsQrcodePopover = require('components/pop/apps_qrcode'),
        CommonQrcodePopover = require('components/pop/common_qrcode'),
        ImageTextPopover = require('components/pop/image_text'),
        ImportPopover = require('components/pop/import'),
        AlertPopover = require('components/pop/alert'),
        ContentPopover = require('components/pop/content');

    var cached = {};
    return {
        initialize: function(obj) {
			var target = obj.target = $(obj.target),
                className = obj.className,
                type = obj.type,
                callback = obj.callback,
                trigger = obj.trigger = $(obj.trigger || target),
                data = obj.data,
                notAutoHide = obj.notAutoHide || false,
                content = obj.content || '',
                appendTarget = obj.appendTarget || 'body';

            if (cached[type]) {
                cached[type].reset(obj);
                return cached[type];
            }

            var popover;
            switch (type) {
                case 'link':
                    popover = new LinkPopover({
                        callback: callback,
                        target: target,
                        trigger: trigger,
                        content: content
                    });
                    break;
                case 'text':
                    popover = new TextPopover({
                        callback: callback,
                        target: target,
                        trigger: trigger
                    });
                    break;
                case 'delete':
                    popover = new DeletePopover({
                        callback: callback,
                        target: target,
                        className: className || 'popover left',
                        trigger: trigger
                    });
                    break;
                case 'confirm':
                    popover = new ConfirmPopover({
                        callback: callback,
                        target: target,
                        className: className || 'popover right',
                        trigger: trigger,
                        notAutoHide: notAutoHide,
                        data: data,
                        content: content,
                    });
                    break;
                case 'alert':
                    popover = new AlertPopover({
                        callback: callback,
                        target: target,
                        className: className || 'popover right',
                        trigger: trigger,
                        data: data,
                        content: content
                    });
                    break;
                case 'note':
                    popover = new NotePopover({
                        callback: callback,
                        target: target,
                        className: className || 'popover right',
                        trigger: trigger,
                        data: data
                    });
                    break;
                case 'timer':
                    popover = new TimerPopover({
                        callback: callback,
                        target: target,
                        className: className || 'popover top',
                        trigger: trigger,
                        data: data
                    });
                    break;
                case 'delete_multiple':
                    popover = new DeleteMultiplePopover({
                        callback: callback,
                        target: target,
                        trigger: trigger
                    });
                    break;
                case 'url':
                    popover = new UrlPopover({
                        callback: callback,
                        target: target,
                        trigger: trigger,
                        data: data
                    });
                    break;
                case 'goods_url':
                    popover = new GoodsUrlPopover({
                        callback: callback,
                        target: target,
                        trigger: trigger,
                        data: data
                    });
                    break;
                case 'rule':
                    popover = new RulePopover({
                        callback: callback,
                        target: target,
                        trigger: trigger,
                        className: className,
                        data: data
                    });
                    break;
                case 'keyword':
                    popover = new KeywordPopover({
                        callback: callback,
                        target: target,
                        trigger: trigger,
                        // notAutoHide: true,
                        data: data
                    });
                    break;
                case 'reply':
                    popover = new ReplyPopover({
                        callback: callback,
                        target: target,
                        trigger: trigger,
                        className: className,
                        notAutoHide: true,
                        data: data
                    });
                    break;
                case 'normal_reply':
                    popover = new NormalReplyPopover({
                        callback: callback,
                        target: target,
                        trigger: trigger,
                        // notAutoHide: true,
                        className: className,
                        data: data
                    });
                    break;
                case 'rename':
                    popover = new RenamePopover({
                        callback: callback,
                        target: target,
                        className: className || 'popover left',
                        trigger: trigger,
                        data: data
                    });
                    break;
                case 'chosen':
                    popover = new ChosenPopover({
                        callback: callback,
                        target: target,
                        trigger: trigger,
                        notAutoHide: true,
                        data: data
                    });
                    break;
                case 'change_category':
                    popover = new ChangeCategoryPopover({
                        callback: callback,
                        target: target,
                        trigger: trigger
                    });
                    break;
                case 'change_category2':
                    popover = new ChangeCategory2Popover({
                        callback: callback,
                        target: target,
                        trigger: trigger,
                        className: className || 'popover top',
                        data: data
                    });
                    break;
                case 'change':
                    popover = new ChangePopover({
                        callback: callback,
                        target: target,
                        className: className || 'popover top',
                        trigger: trigger
                    });
                    break;
                case 'help_notes':
                    popover = new HelpNotesPopover({
                        callback: callback,
                        target: target,
                        className: className || 'popover bottom',
                        trigger: trigger
                    });
                    break;
                case 'memo':
                    popover = new MemoPopover({
                        callback: callback,
                        target: target,
                        trigger: trigger,
                        // notAutoHide: true,
                        className: className || 'popover left',
                        data: data
                    });
                    break;
                case 'import':
                    popover = new ImportPopover({
                        callback: callback,
                        target: target,
                        trigger: trigger,
                        className: className || 'popover left',
                        data: data
                    });
                    break;
                case 'category':
                    popover = new CategoryPopover({
                        callback: callback,
                        target: target,
                        trigger: trigger,
                        data: data
                    });
                    break;
                case 'msgcat':
                    popover = new MsgCatPopover({
                        callback: callback,
                        target: target,
                        trigger: trigger,
                        data: data
                    });
                    break;
                case 'tag':
                    popover = new TagPopover({
                        callback: callback,
                        target: target,
                        trigger: trigger,
                        data: data
                    });
                    break;
                case 'form_actions_confirm':
                    popover = new FormActionsPopover({
                        callback: callback,
                        target: target,
                        appendTarget: appendTarget,
                        className: className || 'popover top',
                        trigger: trigger,
                        data: data
                    });
                    break;
                case 'send_goods':
                    popover = new SendGoodsPopover({
                        callback: callback,
                        target: target,
                        trigger: trigger,
                        className: 'popover left'
                    });
                    break;
                case 'cube':
                    popover = new CubePopover({
                        callback: callback,
                        target: target,
                        trigger: trigger,
                        className: 'popover bottom',
                        data: data
                    });
                    break;
                case 'fans_tag':
                    popover = new FansTagPopover({
                        callback: callback,
                        target: target,
                        trigger: trigger,
                        className: 'popover bottom',
                        data: data
                    });
                    break;
                case 'fans_level':
                    popover = new FansLevelPopover({
                        callback: callback,
                        target: target,
                        trigger: trigger,
                        className: 'popover bottom',
                        data: data
                    });
                    break;
                case 'qrcode':
                    popover = new QrcodePopover({
                        callback: callback,
                        target: target,
                        trigger: trigger,
                        className: 'popover bottom',
                        data: data
                    });
                    break;
                case 'rule_reply':
                    popover = new RuleReplyPopover({
                        callback: callback,
                        target: target,
                        trigger: trigger,
                        className: className,
                        notAutoHide: true,
                        data: data
                    });
                    break;
                case 'rule_method':
                    popover = new RuleMethodPopover({
                        callback: callback,
                        target: target,
                        trigger: trigger,
                        className: className,
                        data: data
                    });
                    break;
                case 'apps_qrcode':
                    popover = new AppsQrcodePopover({
                        target: target,
                        trigger: trigger,
                        className: className || 'popover left',
                        data: data
                    });
                    break;
                case 'common_qrcode':
                    popover = new CommonQrcodePopover({
                        target: target,
                        trigger: trigger,
                        className: className || 'popover left',
                        data: data
                    });
                    break;
                case 'image_text':
                    popover = new ImageTextPopover({
                        target: target,
                        trigger: trigger,
                        className: className || 'popover top',
                        data: data
                    });
                    break;
                case 'content':
                    popover = new ContentPopover({
                        target: target,
                        trigger: trigger,
                        className: className || 'popover top',
                        data: data
                    });
                    break;
                default:
                    break;
            }

            popover.render().$el.appendTo(appendTarget);
            popover.positioning();

            if(!_.contains(['help_notes', 'confirm', 'apps_qrcode', 'common_qrcode', 'change_category2'], type)) {
                // 解决宽度不同的pop在同一页面上的错位问题
                cached[type] = popover;
            }
            return popover;
        }
    };
});
